/*
 * Copyright (c) 2008, Yifu Huang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of SleekIce nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.sleekice.gui;

import com.sleekice.util.GL;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class TextButton extends Button {
    /* text color */
    private static final Color DEF_TEXT_C = Color.darkGray;
    private Color textC;
    
    /* text and font */
    private static final Font DEF_FONT =
        new Font("Verdana", Font.PLAIN, 12);
    private Font font;
    private static final String DEF_TEXT = "Button";
    private String text;
    private int drawX; /* this is the x coordinate to draw the text */
    private int drawY; /* this is the y coordinate to draw the text */
    
    /* offsets for the button text from the button border */
    private static final int DEF_LEFT_SPC = 3;
    private static final int DEF_RIGHT_SPC = 3;
    private static final int DEF_TOP_SPC = 3;
    private static final int DEF_BOTTOM_SPC = 3;
    private int leftSpc = DEF_LEFT_SPC;
    private int rightSpc = DEF_RIGHT_SPC;
    private int topSpc = DEF_TOP_SPC;
    private int bottomSpc = DEF_BOTTOM_SPC;
    
    public TextButton(int x, int y) {
        this(DEF_TEXT,x,y);
    }
    public TextButton(String text, int x, int y) {
        super(x,y);
        
        textC = DEF_TEXT_C;
        
        font = DEF_FONT;
        this.text = text;
        
        FontMetrics fontMetrics = GL.g.getFontMetrics(font);
        int width = fontMetrics.stringWidth(text) + leftSpc + rightSpc;
        int height = fontMetrics.getHeight() + topSpc + bottomSpc;
        bounds = new Rectangle(x, y, width, height);
        drawX = x + leftSpc;
        drawY = fontMetrics.getAscent() + topSpc + y;
    }
    
    /**
     * Centers the text in this button. This won't break any spacing rules
     * because anything to do with resizing makes sure the text fits within
     * the spacings.
     */
    public void centerText() {
        FontMetrics fontMetrics = GL.g.getFontMetrics(font);
        drawX = bounds.width/2+bounds.x-fontMetrics.stringWidth(text)/2;
        drawY = bounds.height/2+bounds.y-fontMetrics.getHeight()/2+fontMetrics.getAscent();
    }
    
    @Override
    public void render(Graphics2D g) {
        super.render(g);
        g.setColor(textC);
        g.setFont(font);
        g.drawString(text, drawX + (int) offsetPoint.getX(),
            drawY + (int) offsetPoint.getY());
    }
    
    @Override
    public void resize(int width, int height) {
        FontMetrics fontMetrics = GL.g.getFontMetrics(font);
        int changeWidth = width - bounds.width;
        int changeHeight = height - bounds.height;

        /**
         * If the width is decreasing, make sure the text can fit.
         */
        if (changeWidth < 0) {
            fontMetrics = GL.g.getFontMetrics(font);
            while (fontMetrics.stringWidth(text) > width - leftSpc - rightSpc) {
                /**
                 * Decrease font size.
                 */
                font = font.deriveFont(font.getSize() - 1f);
                fontMetrics = GL.g.getFontMetrics(font);
            }
        }

        /**
         * If the height is decreasing, make sure the text can fit.
         */
        if (changeHeight < 0) {

            fontMetrics = GL.g.getFontMetrics(font);
            while (fontMetrics.getHeight() > height - topSpc - bottomSpc) {
                /**
                 * If no more spacing is left, decrease font size.
                 */
                font = font.deriveFont(font.getSize() - 1f);
                fontMetrics = GL.g.getFontMetrics(font);
            }
        }
        drawX = bounds.x + leftSpc;
        drawY = fontMetrics.getAscent() + topSpc + bounds.y;
        
        super.resize(width, height);
    }
}
